//
//  ZyaLogisticsFilterView.swift
//  SlaughterClient
//
//  Created by hudan on 2025/8/13.
//  Copyright © 2025 zwb. All rights reserved.
//

import UIKit

final class ZyaLogisticsFilterView: UIView {
    // 排序, 剩余账期, 状态
    var searchBlock: ((_ sortType: String, _ period: String, _ status: String) -> Void)?
    
    private var currSortBtn: UIButton! {
        didSet {
            currSortBtn.isSelected = true
            currSortBtn.extBgColor(color: Theme.primaryColor())
        }
    }
    private var currPeriodBtn: UIButton! {
        didSet {
            currPeriodBtn.isSelected = true
            currPeriodBtn.extBgColor(color: Theme.primaryColor())
        }
    }
    private var currStatusBtn: UIButton! {
        didSet {
            currStatusBtn.isSelected = true
            currStatusBtn.extBgColor(color: Theme.primaryColor())
        }
    }
    
    private let contentView = UIView().extBgColor(color: .white)
        .extFrame(frame: .init(x: KS_SCREEN_WIDTH, y: StatusBarHeight + 44, width: KS_SCREEN_WIDTH - 60, height: KS_SCREEN_HEIGHT - (StatusBarHeight + 44)))
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() {
        extBgColor(color: .init(white: 0, alpha: 0.01))
        
        let touchBtn = UIButton().addTouchUpInSideAction { [weak self] _ in
            self?.hide()
        }
        
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.top.bottom.right.equalToSuperview()
            make.left.equalToSuperview().offset(KS_SCREEN_WIDTH)
        }
        
        
        var positionY: CGFloat = 15
        
        let sorts = ["发货日期", "剩余账期"]
        currSortBtn = layout(title: "排序", items: sorts, action: #selector(sortBtnClick(sender:)), positionY: &positionY)
        
        
        let periods = ["全部", "3日内", "7日内", "1月内", "3月内"]
        currPeriodBtn = layout(title: "剩余账期", items: periods, action: #selector(periodBtnClick(sender:)), positionY: &positionY)
        
        
        let statuss = ["全部", "运输中", "已签收", "待付款", "已付款"]
        currStatusBtn = layout(title: "状态", items: statuss, action: #selector(statusBtnClick(sender:)), positionY: &positionY)
        
        
        let resetBtn = UIButton().easy("重置", UIColor(kshex: "333333"), .systemFont(ofSize: 20, weight: .medium), nil, self, #selector(resetBtnClick))
        contentView.addSubview(resetBtn)
        resetBtn.snp.makeConstraints { make in
            make.left.bottom.equalToSuperview()
            make.right.equalTo(contentView.snp.centerX)
            make.height.equalTo(40)
        }

        let searchBtn = UIButton().easy("确定", .white, .systemFont(ofSize: 20, weight: .medium), nil, self, #selector(searchBtnClick))
            .extBgColor(color: Theme.primaryColor())
        contentView.addSubview(searchBtn)
        searchBtn.snp.makeConstraints { make in
            make.right.bottom.equalToSuperview()
            make.left.equalTo(contentView.snp.centerX)
            make.height.equalTo(40)
        }
    }
    
    private func layout(title: String, items: [String], action: Selector, positionY: inout CGFloat) -> UIButton {
        
        var topOffset = positionY
        
        let titleLab = makeTitleLab(title: title)
        contentView.addSubview(titleLab)
        titleLab.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(topOffset + 15)
            make.right.equalToSuperview()
            make.height.equalTo(20)
        }
        
        topOffset += 15 + 20 + 10
        
        var firstItem = UIButton()
        
        for (idx, text) in items.enumerated() {
            let item = makeItem(title: text, tag: idx, action: action)
            contentView.addSubview(item)
            item.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(30)
                make.size.equalTo(item.ks_size)
                
                if 30 + CGFloat(idx)*(item.ks_width + 10) + 15 > contentView.ks_width {
                    topOffset += 30 + 10
                }
                make.top.equalTo(topOffset)
            }
            
            if idx == 0 {
                firstItem = item
            }
        }
        
        positionY = topOffset
        positionY += 10
        
        return firstItem
    }
    
    private func makeTitleLab(title: String) -> UILabel {
        return UILabel().easy(title, UIColor(kshex: "333333"), .systemFont(ofSize: 17, weight: .medium))
    }
    
    private func makeItem(title: String, tag: Int, action: Selector) -> UIButton {
        let btn = UIButton().easy(title, UIColor(kshex: "#666666"), .systemFont(ofSize: 16, weight: .regular), nil, self, action)
            .extTag(tag: tag)
            .extRadius(radius: 15).extMaskBounds(mask: true)
            .extBorderW(width: 0.8).extBorderColor(color: UIColor(kshex: "BBBBBB"))
            .extFrameSize(size: .init(width: 80, height: 30))
        return btn
    }
    
    
    func show(onView: UIView) {
        
        onView.addSubview(self)
        
        UIView.animate(withDuration: 0.3) {
            
            self.extBgColor(color: .init(white: 0, alpha: 0.4))
            
            self.contentView.ks_x = 60
        }
    }
    
    private func hide() {
        UIView.animate(withDuration: 0.3) {
            self.contentView.ks_x = KS_SCREEN_WIDTH
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
    
    //MARK: - Button Click
    @objc private func sortBtnClick(sender: UIButton) {
        
        if sender == currSortBtn {
            return
        }
    }
    
    @objc private func periodBtnClick(sender: UIButton) {
        
    }
    
    @objc private func statusBtnClick(sender: UIButton) {
        
    }
    
    @objc private func resetBtnClick() {
        
    }
    
    @objc private func searchBtnClick() {
        
        let sort = currSortBtn.titleLabel!.text.unwrap
        let period = currPeriodBtn.titleLabel?.text == "全部" ? "" : currPeriodBtn.titleLabel!.text.unwrap
        let status = currStatusBtn.titleLabel?.text == "全部" ? "" : currStatusBtn.titleLabel!.text.unwrap
        searchBlock?(sort, period, status)
        
        hide()
    }
}
